# 1.声明所需的cmake版本
cmake_minimum_required(VERSION 3.0.0)
# 2.定义项目工程名称
project(friend_server)

set(target "friend_server")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")

# 3.检测并生成proto代码框架
    # 3.1.添加所需的proto代码文件名称
    set(proto_path ${CMAKE_CURRENT_SOURCE_DIR}/../proto)
    set(proto_files message.proto base.proto user.proto friend.proto)
    # 3.2.检测代码框架
    set(proto_hxx "")
    set(proto_cxx "")
    set(proto_srcs "")
    # 3.3.如果没有生成，则预定义生成指令  -- 构建项目之间的生产框架代码
    foreach(proto_file ${proto_files})
        string(REPLACE ".proto" ".pb.cc" proto_cc ${proto_file})
        string(REPLACE ".proto" ".pb.h" proto_hh ${proto_file})
        if(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}${proto_cc})    # 代码文件的检测， 存在就跳过
            add_custom_command(
                PRE_BUILD 
                COMMAND protoc
                ARGS --cpp_out=${CMAKE_CURRENT_BINARY_DIR} -I ${proto_path} --experimental_allow_proto3_optional ${proto_path}/${proto_files}
                DEPENDS ${proto_path}/${proto_file}
                OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${proto_cc}
                COMMENT "生成protobuf框架代码文件:" ${CMAKE_CURRENT_BINARY_DIR}/${proto_cc}
            )
        endif()
    # 3.4.生成框架源码文件名称保存起来
        list(APPEND proto_srcs ${CMAKE_CURRENT_BINARY_DIR}/${proto_cc}) # proto_srcs 存储所有的proto_cc文件到proto_srcs中
    endforeach()

# 3.检测并生成ODB代码框架
    # 3.1.添加所需的odb映射代码文件名称
    set(odb_path ${CMAKE_CURRENT_SOURCE_DIR}/../odb)
    set(odb_files chat_session.hxx chat_session_member.hxx friend_apply.hxx relation.hxx)
    # 3.2.检测代码框架
    set(odb_hxx "")
    set(odb_cxx "")
    set(odb_srcs "")
    # 3.3.如果没有生成，则预定义生成指令  -- 构建项目之间的生产框架代码
    foreach(odb_file ${odb_files})
        string(REPLACE ".hxx" "-odb.hxx" odb_hxx ${odb_file})
        string(REPLACE ".hxx" "-odb.cxx" odb_cxx ${odb_file})
        if(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}${odb_cxx})    # 代码文件的检测， 存在就跳过
            add_custom_command(
                PRE_BUILD 
                COMMAND odb
                ARGS -d mysql --std c++11 --generate-query --generate-schema --profile boost/date-time ${odb_path}/${odb_file}
                DEPENDS ${odb_path}/${odb_file}
                OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${odb_cxx}
                COMMENT "生成ODB框架代码文件:" ${CMAKE_CURRENT_BINARY_DIR}/${odb_cxx}
            )
        endif()
    # 3.4.生成框架源码文件名称保存起来
        list(APPEND odb_srcs ${CMAKE_CURRENT_BINARY_DIR}/${odb_cxx}) # odb_srcs 存储所有的cxx文件到odb_srcs中
    endforeach()


# 4.获取源码目录下的所有源码文件
set(src_files "")
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/source src_files)
# 5.声明目标和依赖 --- 库的引入
add_executable(${target} ${src_files} ${proto_srcs} ${odb_srcs})

# # 测试模块
# set(test_client "friend_client")
# set(test_files "")
# aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/test test_files)
# add_executable(${test_client} ${test_files} ${proto_srcs})
# target_link_libraries(${test_client}  -pthread -lgflags -lspdlog -lfmt -lbrpc -lssl -lcrypto -lprotobuf
#     -lleveldb -letcd-cpp-api -lcpprest -lcurl -lodb -lodb-mysql -lodb-boost /usr/lib/x86_64-linux-gnu/libjsoncpp.so.19 
#      -lelasticlient  -L/usr/local/lib -lgtest)

# 6.设置头文件默认搜索路径
include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../odb/) 
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../common/) 
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../third/include)
# 7.设置需要连接的库
target_link_libraries(${target} -lgflags -lspdlog -lfmt -lbrpc -lssl -lcrypto -lprotobuf -lleveldb 
    -letcd-cpp-api -lcpprest -lcurl -lodb -lodb-mysql -lodb-boost /usr/lib/x86_64-linux-gnu/libjsoncpp.so.19 
    -lelasticlient)
# 8.设置安装路径
INSTALL(TARGETS ${target} ${test_client} RUNTIME DESTINATION bin)

# mkdir build
# cd build
# cmake ..
# make

# rm -rf ./* 快速删除build中的内容
# cmake ..
# make
# 再次构建
